package com.nulldev.util.uri.yt.broken;

import java.io.File;
import java.util.List;
import java.util.regex.Pattern;

import com.nulldev.util.java.swing.nGetUI;
import com.nulldev.util.logging.LoggerFactory;
import com.nulldev.util.uri.URIUtil;
import com.nulldev.util.uri.nget.nGet;
import com.nulldev.util.uri.yt.broken.util.YTD2Util;

@Deprecated
public class YTDownloader2 {
	public YTDownloader2() {
	}
	
	public List<FmtStreamMap> fetchMap(final String vid) {
//		final String filtered = StringsUtil.remove(
//				StringsUtil.remove(vid, "https://hooktube.com/watch?v="),
//				"https://youtube.com/watch?v=");
				//vid.replace(Pattern.quote("https://hooktube.com/watch?v="), "")
				//.replace(Pattern.quote("https://youtube.com/watch?v="), "");
		try {
			final String[] vidID = vid.split(Pattern.quote("watch?v="));
			return YTD2Util.fetchYoutube(vidID[1]);
		}catch (Exception e) {
			return YTD2Util.fetchYoutube(vid);
		}
	}
	
	public String fetchDownloadLink(final FmtStreamMap map) {
		return YTD2Util.parseDownloadUrl(map);
	}
	
	public void download(final FmtStreamMap map) {
		final nGet nget = new nGet(URIUtil.safeCreate((map.encrypted ? fetchDownloadLink(map) : map.url)));
		nget.overrideName("videoplayback");
		final nGetUI ngui = nGetUI.main(nget);
		nget.download();
		nget.waitTillFinished();
		ngui.setVisible(false);
		ngui.dispose();
	}
	
	public void download(final FmtStreamMap map, final String fileName) {
		final nGet nget = new nGet(URIUtil.safeCreate((map.encrypted ? fetchDownloadLink(map) : map.url)));
		nget.overrideName(fileName);
		final nGetUI ngui = nGetUI.main(nget);
		nget.download();
		nget.waitTillFinished();
		ngui.setVisible(false);
		ngui.dispose();
	}
	
	public void download(final FmtStreamMap map, final String fileName, final File folder) {
		if (folder == null || !folder.isDirectory()) {
			LoggerFactory.getLogger().error("download(map, fileName, folder) -> folder was null!");
			return;
		}
		final nGet nget = new nGet(URIUtil.safeCreate((map.encrypted || map.s != null ? fetchDownloadLink(map) : map.url)));
		nget.setDirectory(folder);
		nget.overrideName(fileName);
		final nGetUI ngui = nGetUI.main(nget);
		nget.download();
		nget.waitTillFinished();
		ngui.setVisible(false);
		ngui.dispose();
	}

}
